(function ($) {
    $('#addJob2').on('click', function () {
        if ($(this).attr('data-status') != 0) {
            return false
        }
        $(this).attr('data-status', 1)
        var title = $('.addNews__title').val(),
            fileUrl = $('.addNews__hide').val(),
            content = $('#addNewsDetailTextarea').val(),
            userInfo = cacheGet('userInfo'); 
        setTimeout(function () {
            $('.addNews__alert').fadeOut()
        }, 1500)
        if (userInfo == 'undefined' || userInfo.token == 'undefined') {
            $('.addNews__alert').fadeIn().text('账号未登录！');
            return false;
        }

        if (title == '') {
            $('.addNews__alert').fadeIn().text('请输入标题')
            return false 
        }

        if (fileUrl == '') {
            $('.addNews__alert').fadeIn().text('请选择文件')
            return false
        }
        if (content == '') {
        $('.addNews__alert').fadeIn().text('请编辑内容')
            return false
        }
        var data = {
            app_access: 'qSoiKwpWVge4TkaryH6MKvOdceGt7ZMaF20g8H0cnXI',
            token: userInfo.token,
            type: userInfo.type,
            title: title,
            thumb: fileUrl,
            content: content

        }

        $.ajax({
            url: '/api/news/add',
            type:'post',
            data:data,
            dataType:'json',
            success: function (res) {
                $('.addNews__alert').fadeIn().text('发布成功!')
                setTimeout(function () {
                    window.location.href = "../index.html"
                }, 1000)
            },
            fail: function () {
                $('.addNews__alert').fadeIn().text('发布失败!')
            }
        })

    })
    $('.addNews__file').on('change', function (e) {
        var files = e.target.files[0] || e.dataTransfer.files[0];
        if (files) {
            var fileLoader = new FileReader();
            fileLoader.readAsDataURL(files);
            fileLoader.onload = function(e) {
                fileLoader.onloadend = function (e) {
                    var bossFile = document.getElementById('addNews__file').files[0];
                    var formData = new FormData();
                    var userInfo = cacheGet('userInfo')
                    formData.append('app_access', 'qSoiKwpWVge4TkaryH6MKvOdceGt7ZMaF20g8H0cnXI');
                    formData.append('token', userInfo.token);
                    formData.append('img', bossFile);
                    if (bossFile) {
                        $.ajax({
                            url:MC.HOST+'/api/member/upload/img',
                            type:'post',
                            data: formData,
                            contentType: false,
                            processData: false,
                            dataType:'json',
                            success: function (res) {
                                $('.addNewsImg').css({'background-image': 'url('+res.data.img+')'}).show()
                                $('.addNews__hide').val(res.data.img)
                            },
                            fail: function () {
                                alertMsg('发布失败!')
                            }
                        })
                    }
                }
            }
        }
    })
})(jQuery)